<?php

class GlobalController extends GTL_Action {

    public function init() {
        parent::init();
            /* Initialize action controller here */
            $this->list_sess_unset('controller_contactus');
            $this->session_search_key = 'controller_contactus';
            $this->session_search_name = 'search_label';
    }

    
   public function indexAction() {
			$successmessage = '';
			$ctc_id = '';
			$imageName = '';
            $errorMessageArray = Array();
            $data = array();
            $contactData = Array();
            $this->view->mode = 'Add';
			$contactForm = new Form_ContactForm();
			
			
			
			//Load contact Model
			$contactModel = new Model_Contact();
			
			//Load contact Form
            $contactForm = new Form_ContactForm();

            //Load the business array for the select box in form 
			//$this->view->form = $contactForm;
            //$businessArray = $contactModel->getBusinessArray();
           // $contactForm->ctc_subject->addMultiOptions($businessArray);
			
			if ($this->getRequest()->isPost()) {
			
			//Check for Errors
                $postedData = $this->_request->getPost();
                if (!$contactForm->isValid($postedData)) {
					//echo 'in';exit;

                    $errorMessage = $contactForm->getMessages();
                    foreach ($errorMessage as $_err) {
                        foreach ($_err as $_val) {
                            $errorMessageArray[] = $_val;
                        }
                    }
                } else {
                   
                    if (count($errorMessageArray) > 0) {

                    } 
					else {
							
						/* send mail */	
						$body = "<html>
							<head>
							</head>
							<body>
								<p>Hello Admin</p>
								<p>You have received a new message from ".$postedData['ctc_name']." </p>
									<p>Subject: ".$postedData['ctc_subject']."</p>
									<p>E-mail id: ".$postedData['ctc_email']."</p>
									<p>Message: ".$postedData['ctc_message']."</p>
							</body>
							</html>
							";
							//echo $body;exit;	
						$metaDataModel = new Model_Metadata();
						$ADMIN_MAIL_DATA = $metaDataModel->fetchEntryByKey("ADMIN_FROM_EMAIL");
						$ADMIN_MAIL = $ADMIN_MAIL_DATA['mtd_value'];
						
						$ADMIN_FROM_DATA = $metaDataModel->fetchEntryByKey("ADMIN_FROM_NAME");
						$ADMIN_FROM = $ADMIN_FROM_DATA['mtd_value'];
						
						$mail = new Zend_Mail();
						$mail->setBodyText($body);
						$mail->setBodyHtml($body, "iso-8859-1");
						$mail->addTo($ADMIN_MAIL, $ADMIN_FROM);
						$mail->setReplyTo($postedData['ctc_email'], $postedData['ctc_name']);
						$mail->addTo($ADMIN_MAIL, $ADMIN_FROM);
						$mailSubject = "Contact Us";
						$mail->addHeader('MIME-Version', '1.0');
						$mail->addHeader('Content-Transfer-Encoding', '8bit');
						$mail->addHeader('X-Mailer:', 'PHP/' . phpversion());
						$mail->setSubject($mailSubject);

						
								
                        if ($this->view->mode == 'Add') {
                            $contactData['ctc_createddate'] = date('Y-m-d H:i:s');
                        }
                        //blog save
                        $contactData['ctc_id'] = $ctc_id;
                        $contactData['ctc_subject'] = $postedData['ctc_subject'];
                        $contactData['ctc_name'] = $postedData['ctc_name'];				
						$contactData['ctc_email'] = $postedData['ctc_email'];
                        $contactData['ctc_message'] = $postedData['ctc_message'];
						if (array_key_exists('ctc_send' , $_POST)){
							$contactData['ctc_send'] = $postedData['ctc_send'];
						}
                        $ctc_id = $contactModel->save($contactData, 'ctc_id');
						$contactForm->reset();
						
                       try {
							$mail->send();
							$successmessage ='Thanks for sending email.';
							$this->addSuccessMessage($successmessage);
							
							$this->_helper->redirector('index', 'global', 'default');
						} catch (Zend_Exception $e) {
							$errorMessage[] = "Due to technical problem, email has not been sent. Please try later or contact webmaster.";
						}
						
                        //$this->addSuccessMessage($successmessage);
                    }
                }
            }
				$this->view->ctc_id = $ctc_id;
				$this->view->errorMessageArray = $errorMessageArray;
				$this->view->succesMessage = $successmessage;
				$this->view->form = $contactForm;
			}	
    }

?>
